// pages/consultationList/index.js
const app = getApp()
let {
  baseUrl,
  commonUpdate,
  commonUpload
} = app.globalData
import {
  ApiGetDepartmentDoctorList
} from '../../api/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl,
    commonUpdate,
    commonUpload,
    index1: "-1",
    active: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let titleDetail = JSON.parse(options.titleDetail)
    this.setData({
      titleDetail,
      title: JSON.parse(options.titleDetail).name,
      masterTitle: options.title
    })
    //获取医生详情
    this.getDepartmentDoctor(titleDetail.id)
    var arr = []
    for (let i = 0; i < 7; i++) {
      arr.push(this.dealTime(i))
    }
    this.setData({
      weekInfo: arr // 赋值给data
    })
    wx.showLoading({
      title: '加载中'
    })
  },

  getDepartmentDoctor(id, date) {
    ApiGetDepartmentDoctorList({
      sub_department_id: id,
      type: wx.getStorageSync('order_type'),
      date
    }).then(res => {
      this.setData({
        dateList: res.data.dateList,
        department: res.data.department,
        doctorList: res.data.doctorList
      })
      wx.hideLoading()
    })
  },
  //选择几号数据的方法
  DatetabSelect(e) {
    this.setData({
      index1: e.currentTarget.dataset.info,
      active: 1
    })
    this.getDepartmentDoctor(this.data.titleDetail.id, e.currentTarget.dataset.id.date)
  },
  //切换几号数据
  allDocClick(e) {
    this.setData({
      index1: '-1',
      active: 0
    })
    this.getDepartmentDoctor(this.data.titleDetail.id)
  },

  //跳转到医生详情界面
  doctorListClick(e) {
    console.log(e)
    console.log(this.data.index1)
    let date = this.data.index1 == '-1' ? this.data.dateList[0].date : this.data.dateList[this.data.index1].date
    wx.navigateTo({
      url: `/pages/doctorDetails/index?id=${e.currentTarget.dataset.v.id}&&index1=${this.data.index1}&&date=${date}&&jiezhen=${e.currentTarget.dataset.v.jiezhen}&&shouzhen=${e.currentTarget.dataset.v.shouzhen}`,
    })
  },



  dealTime: function (num) { // num：未来天数
    var time = new Date() // 获取当前时间日期
    var date = new Date(time.setDate(time.getDate() + num)).getDate() //这里先获取日期，在按需求设置日期，最后获取需要的
    var year = time.getFullYear() //获取年份
    var month = time.getMonth() + 1 // 获取月份
    var day = time.getDay() //  获取星期
    switch (day) { //  格式化
      case 0:
        day = "日"
        break
      case 1:
        day = "一"
        break
      case 2:
        day = "二"
        break
      case 3:
        day = "三"
        break
      case 4:
        day = "四"
        break
      case 5:
        day = "五"
        break
      case 6:
        day = "六"
        break
    }
    var obj = {
      // date: date,
      day: date,
      // month: month,
      week: day,
      date: year + '-' + month + '-' + date
    }
    return obj // 返回对象
  },
  //科室简介点击按钮
  handleDesc(e) {
    //跳转到科室简介的界面
    wx.navigateTo({
      url: `/pages/departmentProfile/index?data=${e.currentTarget.dataset.v}&&titel=${this.data.department.name}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})